import {createRouter, createWebHistory} from 'vue-router'
import store from '../store/index'

const routes = [
    {
        path: '/',
        component: () => import('../views/Home')
    },
    {
        path: '/itemmusic',
        component: () => import('../views/ItemMusic')
    },
    {
        path: '/search',
        component: () => import('../views/Search')
    },
    {
        path: '/login',
        component: () => import('../views/Login')
    },
    {
        path: '/userinfo',
        beforeEnter: (to, from, next) => {
            if (store.state.user.isLogin || store.state.user.token || localStorage.getItem('token')) {
                next()
            } else {
                next('/login')
            }
        },
        component: () => import('../views/UserInfo'),
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

// 全局路由守卫
router.beforeEach((to) => {
    // 根据路由地址，切换底部播放组件的显隐
    // 去登陆和个人中心时不显示底部组件
    if (to.path == '/login' || to.path == '/userinfo') {
        // console.log(from)
        store.state.user.isShowFooterMusic = false
    } else {
        store.state.user.isShowFooterMusic = true
    }
})

export default router
